package com.easily.factory;

import com.easily.factory.nfactoryTest.ObjectBean;
import com.easily.system.exception.CarrotException;
import com.easily.system.util.AnnotationUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;

public abstract class Pool {
    private boolean isOpenPut = true;
    abstract public boolean put(ObjectBean objectBean) throws InvocationTargetException, IllegalAccessException;

    public boolean filter(ObjectBean objectBean){
        if (isOpenPut && AnnotationUtils.hasAnnotation(objectBean.getClazz(),getLabel())){
            try {
                return put(objectBean);
            }catch (Exception e){
                throw new CarrotException("bean入池失败："+objectBean.getClazz().getName());
            }
        }
        return false;
    }

    abstract public Class<? extends Annotation> getLabel();

    abstract public void end();

    public void closePut(){
        this.isOpenPut = false;
    }
}
